package Servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Dao.Admin_Dao;
import Entity.Admin;

public class Admin_List_Servlet extends HttpServlet {

    private Admin_Dao admindao = new Admin_Dao();

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String method = request.getParameter("method");
        String path = "pages/Admin_List.jsp";
        boolean ac = false;
        if (method.equals("list")) {
            List<Admin> list = admindao.select();
            request.setAttribute("list", list);
        } else if (method.equals("update")) {
            int id = Integer.parseInt(request.getParameter("form_id"));
            String password = request.getParameter("form_password");
            admindao.update(id, password);
            path = "Admin_List_Servlet?method=list";
            ac = true;
        } else if (method.equals("add")) {
            String username = request.getParameter("form_username");
            String password = request.getParameter("form_password");
            admindao.add(username, password);
            path = "Admin_List_Servlet?method=list";
            ac = true;
        } else if (method.equals("delete")) {
            int id = Integer.parseInt(request.getParameter("id"));
            admindao.delete(id);
            path = "Admin_List_Servlet?method=list";
            ac = true;
        }
        if (ac) {
            response.sendRedirect(path);
        } else {
            request.getRequestDispatcher(path).forward(request, response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}
